//
//  RegisViewController.m
//  mintHome
//
//  Created by 朱彦名 on 16/6/23.
//  Copyright © 2016年 朱彦名. All rights reserved.
//

#import "RegisViewController.h"
#import "DataBaseManager.h"
#import "NSString+MD5.h"
#import "loginViewController.h"

@interface RegisViewController ()<UITextFieldDelegate>

@property (nonatomic, retain) UIImageView *backImg;
@property (nonatomic, retain) UITextField *userText;
@property (nonatomic, retain) UITextField *passwordText;
@property (nonatomic, retain) UITextField *repasswoedText;
@property (nonatomic, retain) UITextField *mailboxText;
@property (nonatomic, retain) UITextField *phoneText;
@property (nonatomic, retain) UIButton *registerButton;
@property (nonatomic, retain) NSMutableArray *nameArr;

@end

@implementation RegisViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatSubviews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)creatSubviews {
    //backImg
    self.backImg = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _backImg.image = [UIImage imageNamed:@"beijing.jpeg"];
    [self.view addSubview:_backImg];
    [_backImg release];
    
    //userText
    self.userText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 150, self.view.frame.size.height / 19)];
    _userText.center = CGPointMake(self.view.center.x, self.view.frame.size.height / 5);
    _userText.placeholder = @"请输入用户名";
    _userText.clearButtonMode = YES;
    _userText.clearsOnBeginEditing = YES;
    _userText.borderStyle = UITextBorderStyleRoundedRect;
    _userText.delegate = self;
    _userText.tag = 1000;
    [self.view addSubview:_userText];
    [_userText release];
    
    //passwordText
    self.passwordText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, _userText.frame.size.width, _userText.frame.size.height)];
    _passwordText.center = CGPointMake(_userText.center.x, _userText.center.y + _userText.frame.size.height + 15);
    _passwordText.placeholder = @"请输入密码";
    _passwordText.clearButtonMode = YES;
    _passwordText.clearsOnBeginEditing = YES;
    _passwordText.secureTextEntry = YES;
    _passwordText.borderStyle = UITextBorderStyleRoundedRect;
    _passwordText.delegate = self;
    _passwordText.tag = 2000;
    [self.view addSubview:_passwordText];
    [_passwordText release];
    
    //repassWordText
    self.repasswoedText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 150, self.view.frame.size.height / 19)];
    _repasswoedText.center = CGPointMake(self.view.center.x, _passwordText.center.y + _passwordText.frame.size.height + 15);
    _repasswoedText.placeholder = @"请再次输入密码";
    _repasswoedText.clearButtonMode = YES;
    _repasswoedText.clearsOnBeginEditing = YES;
    _repasswoedText.secureTextEntry = YES;
    _repasswoedText.borderStyle = UITextBorderStyleRoundedRect;
    _repasswoedText.delegate = self;
    _repasswoedText.tag = 3000;
    [self.view addSubview:_repasswoedText];
    [_repasswoedText release];
    
    //mailboxText
    self.mailboxText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 150, self.view.frame.size.height / 19)];
    _mailboxText.center = CGPointMake(self.view.center.x, _repasswoedText.center.y + _repasswoedText.frame.size.height + 15);
    _mailboxText.placeholder = @"请输入邮箱";
    _mailboxText.clearButtonMode = YES;
    _mailboxText.clearsOnBeginEditing = YES;
    _mailboxText.borderStyle = UITextBorderStyleRoundedRect;
    _mailboxText.delegate = self;
    _mailboxText.tag = 4000;
    [self.view addSubview:_mailboxText];
    [_mailboxText release];
    
    //phoneText
    self.phoneText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 150, self.view.frame.size.height / 19)];
    _phoneText.center = CGPointMake(self.view.center.x, _mailboxText.center.y + _mailboxText.frame.size.height + 15);
    _phoneText.placeholder = @"请输入电话号";
    _phoneText.clearButtonMode = YES;
    _phoneText.clearsOnBeginEditing = YES;
    _phoneText.borderStyle = UITextBorderStyleRoundedRect;
    _phoneText.delegate = self;
    _phoneText.tag = 5000;
    [self.view addSubview:_phoneText];
    [_phoneText release];
    
    // registerButton
    self.registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _registerButton.frame = CGRectMake(0, 0, self.view.frame.size.width / 4, self.view.frame.size.height / 19);
    _registerButton.center = CGPointMake(self.view.center.x, _phoneText.center.y + _phoneText.frame.size.height + 15);
    _registerButton.layer.cornerRadius = 5;
    _registerButton.backgroundColor = [UIColor purpleColor];
    [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [_registerButton addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    _registerButton.tintColor = [UIColor yellowColor];
    [self.view addSubview:_registerButton];

}

- (void)registerAction: (UIButton *)button{
    
    if (self.userText.text.length != 0 && self.passwordText.text.length != 0 && self.repasswoedText.text.length != 0 && [self.passwordText.text isEqualToString:self.repasswoedText.text] ) {
        
        //判断是否存在用户名
        
        [[DataBaseManager shareInstance]openDB];
        NSMutableArray *arr = [[DataBaseManager shareInstance]select];
        self.nameArr = [NSMutableArray array];
        for (UserModal *m in arr) {

            [self.nameArr addObject:m.userName];
            
        }
//        NSString *newUserMD5Str = [_userText.text stringFromMD5];
//        NSString *newUserNameStr = [newUserMD5Str stringByReplacingOccurrencesOfString:@"0" withString:@"1"];
        
        for (NSString *temp in _nameArr) {
            if ([_userText.text isEqualToString:temp]) {
                
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户已存在" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:^{}];
                
            [self dismissViewControllerAnimated:YES completion:^{}];
            }
        }
    
        DataBaseManager *db = [DataBaseManager shareInstance];
        [db openDB];
        [[DataBaseManager shareInstance]createTable];
        //输入的数据两级加密
        // 给用户名和密码等信息进行MD5加密
//        NSString *userMD5Str = [_userText.text stringFromMD5];
//        NSString *passwordMD5Str = [_passwordText.text stringFromMD5];
//        NSString *mailboxMD5Str = [_mailboxText.text stringFromMD5];
//        NSString *phoneMD5Str = [_phoneText.text stringFromMD5];
//        
//        //二次加密
//        NSString *userNameStr = [userMD5Str stringByReplacingOccurrencesOfString:@"0" withString:@"1"];
//        NSString *passwordStr = [passwordMD5Str stringByReplacingOccurrencesOfString:@"0" withString:@"1"];
//        NSString *mailboxStr = [mailboxMD5Str stringByReplacingOccurrencesOfString:@"0" withString:@"1"];
//        NSString *phoneStr = [phoneMD5Str stringByReplacingOccurrencesOfString:@"0" withString:@"1"];
        
        // 创建存储联系人的Modal
        UserModal *modal = [UserModal new];
//        modal.userName = userNameStr;
//        modal.password = passwordStr;
//        modal.mailbox = mailboxStr;
//        modal.phone = phoneStr;
        
        modal.userName = _userText.text;
        modal.password = _passwordText.text;
        
        // 将联系人的信息存到数据库里面
        [[DataBaseManager shareInstance] insertUser:modal];

        
        
            // 注册成功后返回用户界面
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
            
        [self presentViewController:alertView animated:YES completion:^{
            }];
        [self dismissViewControllerAnimated:YES completion:^{
                
            
            
            [self.navigationController popViewControllerAnimated:YES];
            
            }];
        
        
        
        
    }else {
        
        
            
            // 否则就提示用户名的输入格式不正确
            UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的输入格式不正确!" preferredStyle:UIAlertControllerStyleAlert];
            
            [self presentViewController:alertView animated:YES completion:^{
            }];
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        

}
}
@end
